import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import { CostOfLaborServiceProxy, LaborCostListDto } from '@shared/service-proxies/service-proxies';
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDatepickerConfig, BsDaterangepickerConfig } from 'ngx-bootstrap';
import { CreateOrEditLaborCostModalComponent } from './create-or-edit-labor-cost-modal.component';

@Component({
    templateUrl: './labor-cost.component.html',
    animations: [appModuleAnimation()]
})
export class LaborCostComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    // @ViewChild('excelModal') excelModal: ExcelModalComponent;
    @ViewChild('createOrEditLaborCostModal') createOrEditLaborCostModal: CreateOrEditLaborCostModalComponent;

    anyncing = false;
    filterText = '';
    deviceId?: number;
    workshop = '';
    month?: number;
    year?: number;

    public dateRange: Date[] = [
        moment()
            .startOf('day')
            .toDate(),
        moment()
            .endOf('day')
            .toDate()
    ];
    // userDataPermissions;
    // selectedDepts;
    selectedValues = new Array<LaborCostListDto>();
    // minMode: BsDatepickerViewMode = 'month';

    // bsConfig: Partial<BsDaterangepickerConfig>;
    // extensions: Array<ListDtoExtensionsItem>;
    constructor(injector: Injector, private _costOfLaborServiceProxy: CostOfLaborServiceProxy) {
        super(injector);
    }

    ngOnInit() {}

    getTableData(event?: LazyLoadEvent) {
        // if (!this.selectedDepts) {
        //     return;
        // }
        // if (this.primengTableHelper.shouldResetPaging(event)) {
        //     this.paginator.changePage(0);

        //     return;
        // }

        this.primengTableHelper.showLoadingIndicator();
        this.workshop = '';
        let monthly: number;
        this.month = monthly;
        let years: number;
        this.year = years;
        this._costOfLaborServiceProxy
            .getAll(
                this.workshop,
                this.year,
                this.month,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    getAllAsync(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();

        this._costOfLaborServiceProxy
            .getAll(
                this.workshop,
                this.year,
                this.month,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    createOrEditLaborCost(): void {
        this.createOrEditLaborCostModal.show();
    }
    delete(laborCostListDto: LaborCostListDto) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            let self = this;

            if (isConfirmed) {
                this._costOfLaborServiceProxy.delete(laborCostListDto.id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    self.getAllAsync();
                });
            }
        });
    }
}
